package com.example.zwh.computershop.my_shopp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zwh.computershop.Myapp;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.androidUtil.ActivityCollector;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.sqlModel.Address;

import java.util.ArrayList;

public class AddressActivity extends AppCompatActivity implements View.OnClickListener, AddressAdapter.OnItemClickLitener{

    private TextView add_new_address ,add_address;
    private Intent intent;
    private ImageView address_back2, address_back;

    private RecyclerView addreRecyclerView;
    private AddressAdapter addressAdapter;
    private ArrayList<Address> addresses = new ArrayList<>();

    private Myapp myapp;

    LinearLayout no_address, have_address;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_address);
        ActivityCollector.addActivity(this);
        initView();
        initEvent();
        initData();
    }

    void initView() {
        no_address = findViewById(R.id.no_address);
        have_address = findViewById(R.id.have_address);

        add_new_address = findViewById(R.id.add_new_address);
        address_back2 = findViewById(R.id.address_back2);
        address_back = findViewById(R.id.address_back);
        add_address = findViewById(R.id.add_address);

        myapp = (Myapp) getApplication();

        addreRecyclerView = findViewById(R.id.address_recy);
        addressAdapter = new AddressAdapter(addresses);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        addreRecyclerView.setLayoutManager(layoutManager);
        addreRecyclerView.setAdapter(addressAdapter);
    }

    void initData() {

        //获取地址数据
        Thread t = new Thread(new takeThread());
        t.start();
        try {
            t.join();
        }catch (InterruptedException ie) {
            ie.printStackTrace();
        }
        addresses.clear();
        //放置数据
        handleData.copyList(addresses, myapp.getAddresses());

        //显示不同布局
        if (addresses.size() != 0) {
            have_address.setVisibility(View.VISIBLE);
            no_address.setVisibility(View.GONE);
        }else {
            no_address.setVisibility(View.VISIBLE);
            have_address.setVisibility(View.GONE);
        }
    }

    void initEvent() {
        addressAdapter.setOnItemClickLitener(this);

        add_new_address.setOnClickListener(this);
        address_back2.setOnClickListener(this);
        address_back.setOnClickListener(this);
        add_address.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.address_back :
                finish();
                break;
            case R.id.address_back2 :
                finish();
                break;
            case R.id.add_address :
                Toast.makeText(this, "添加地址", Toast.LENGTH_SHORT).show();
                intent = new Intent(this, AddModifyAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.add_new_address :
                Toast.makeText(this, "添加新地址", Toast.LENGTH_SHORT).show();
                intent = new Intent(this, AddModifyAddressActivity.class);
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onItemClick(int aid) {
        Toast.makeText(this, "修改地址", Toast.LENGTH_SHORT).show();
        AddModifyAddressActivity.actionStart(this, "修改地址", aid);
    }

    class takeThread extends Thread {
        @Override
        public void run() {
            ArrayList<Address> adra = new ArrayList<>();
            int state = handleData.getAddre(adra, myapp.getUser().getId());
            if (state == 3) {
                myapp.getAddresses().clear();
                myapp.setAddresses(adra);
            }else if (state == 1) {
                myapp.getAddresses().clear();
            }
        }
    }

    //可见时重新加载数据
    @Override
    protected void onStart() {
        super.onStart();
        initData();
        addressAdapter.update();
    }
}
